home *** CD-ROM | disk | FTP | other *** search
/ Shareware Super Platinum 8 / Shareware Super Platinum 8.iso / mac / PROGTOOL / GWMALLOC.ZIP;1 / GWMALLOC.TAR / gw_malloc / compat.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-08  |  4.7 KB  |  207 lines

  1. /*
  2.  * compatibility functions for those systems who are missing them.
  3.  *
  4.  * Copyright 1992 by Gray Watson and the Antaire Corporation
  5.  *
  6.  * This file is part of the malloc-debug package.
  7.  *
  8.  * This library is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU Library General Public
  10.  * License as published by the Free Software Foundation; either
  11.  * version 2 of the License, or (at your option) any later version.
  12.  *
  13.  * This library is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  * Library General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Library General Public
  19.  * License along with this library (see COPYING-LIB); if not, write to the
  20.  * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  *
  22.  * The author of the program may be contacted at gray.watson@antaire.com
  23.  */
  24.  
  25. /*
  26.  * This file holds the compatibility routines necessary for the library if
  27.  * your system does not have them.
  28.  */
  29.  
  30. #define MALLOC_DEBUG_DISABLE
  31.  
  32. #include "malloc.h"
  33. #include "malloc_loc.h"
  34.  
  35. #include "compat.h"
  36. #include "conf.h"
  37.  
  38. #if INCLUDE_RCS_IDS
  39. LOCAL    char    *rcs_id =
  40.   "$Id: compat.c,v 1.8 1993/04/05 01:28:54 gray Exp $";
  41. #endif
  42.  
  43. #if HAVE_MEMCPY == 0 && HAVE_BCOPY == 0
  44. /*
  45.  * copy LEN characters from FROM to TO
  46.  */
  47. EXPORT    char    *memcpy(char * to, const char * from, int len)
  48. {
  49.   char    *hold = to;
  50.   
  51.   for (; len > 0; len--, to++, from++)
  52.     *to = *from;
  53.   
  54.   return hold;
  55. }
  56. #endif /* HAVE_MEMCPY == 0 && HAVE_BCOPY == 0 */
  57.  
  58. #if HAVE_MEMCMP == 0 && HAVE_BCMP == 0
  59. /*
  60.  * compare LEN characters, return -1,0,1 if STR1 is <,==,> STR2
  61.  */
  62. EXPORT    int    memcmp(const char * str1, const char * str2, int len)
  63. {
  64.   for (; len > 0; len--, str1++, str2++)
  65.     if (*str1 != *str2)
  66.       return *str1 - *str2;
  67.   
  68.   return 0;
  69. }
  70. #endif /* HAVE_MEMCMP == 0 && HAVE_BCMP == 0 */
  71.  
  72. #if HAVE_MEMSET == 0
  73. /*
  74.  * set LEN characters in STR to character CH
  75.  */
  76. EXPORT    char    *memset(char * str, int ch, int len)
  77. {
  78.   char    *hold = str;
  79.   
  80.   for (; len > 0; len--, str++)
  81.     *str = (char)ch;
  82.   
  83.   return hold;
  84. }
  85. #endif /* HAVE_MEMSET == 0 */
  86.  
  87. #if HAVE_INDEX == 0
  88. /*
  89.  * find CH in STR by searching backwards through the string
  90.  */
  91. EXPORT    char    *index(const char * str, int ch)
  92. {
  93.   for (; *str != NULLC; str++)
  94.     if (*str == (char)ch)
  95.       return str;
  96.   
  97.   return NULL;
  98. }
  99. #endif /* HAVE_INDEX == 0 */
  100.  
  101. #if HAVE_RINDEX == 0
  102. /*
  103.  * find CH in STR by searching backwards through the string
  104.  */
  105. EXPORT    char    *rindex(const char * str, int ch)
  106. {
  107.   char    *pnt = NULL;
  108.   
  109.   for (; *str != NULLC; str++)
  110.     if (*str == (char)ch)
  111.       pnt = str;
  112.   
  113.   return pnt;
  114. }
  115. #endif /* HAVE_RINDEX == 0 */
  116.  
  117. #if HAVE_STRCAT == 0
  118. /*
  119.  * concatenate STR2 onto the end of STR1
  120.  */
  121. EXPORT    char    *strcat(char * str1, const char * str2)
  122. {
  123.   char    *hold = str1;
  124.   
  125.   for (; *str1 != NULLC; str1++);
  126.   
  127.   while (*str2 != NULLC)
  128.     *str1++ = *str2++;
  129.   *str1 = NULLC;
  130.   
  131.   return hold;
  132. }
  133. #endif /* HAVE_STRCAT == 0 */
  134.  
  135. #if HAVE_STRCMP == 0
  136. /*
  137.  * returns -1,0,1 on whether STR1 is <,==,> STR2
  138.  */
  139. EXPORT    int    strcmp(const char * str1, const char * str2)
  140. {
  141.   for (; *str1 != NULLC && *str1 == *str2; str1++, str2++);
  142.   return *str1 - *str2;
  143. }
  144. #endif /* HAVE_STRCMP == 0 */
  145.  
  146. #if HAVE_STRLEN == 0
  147. /*
  148.  * return the length in characters of STR
  149.  */
  150. EXPORT    int    strlen(const char * str)
  151. {
  152.   int    len;
  153.   
  154.   for (len = 0; *str != NULLC; str++, len++);
  155.   
  156.   return len;
  157. }
  158. #endif /* HAVE_STRLEN == 0 */
  159.  
  160. #if HAVE_STRTOK == 0
  161. /*
  162.  * get the next token from STR (pass in NULL on the 2nd, 3rd, etc. calls),
  163.  * tokens are a list of characters deliminated by a character from DELIM.
  164.  * writes null into STR to end token.
  165.  */
  166. EXPORT    char    *strtok(char * str, char * delim)
  167. {
  168.   static char    *last_str = "";
  169.   char        *start, *delimp;
  170.   
  171.   /* no new strings to search? */
  172.   if (str != NULL)
  173.     last_str = str;
  174.   else
  175.     /* have we reached end of old one? */
  176.     if (*last_str == NULLC)
  177.       return NULL;
  178.   
  179.   /* parse through starting token deliminators */
  180.   for (; *last_str != NULLC; last_str++) {
  181.     for (delimp = delim; *delimp != NULLC; delimp++)
  182.       if (*last_str == *delimp)
  183.     break;
  184.     
  185.     /* is the character NOT in the delim list? */
  186.     if (*delimp == NULLC)
  187.       break;
  188.   }
  189.   
  190.   /* did we reach the end? */
  191.   if (*last_str == NULLC)
  192.     return NULL;
  193.   
  194.   /* now start parsing through the string, could be NULLC already */
  195.   for (start = last_str; *last_str != NULLC; last_str++)
  196.     for (delimp = delim; *delimp != NULLC; delimp++)
  197.       if (*last_str == *delimp) {
  198.     /* punch NULL and point last_str past it */
  199.     *last_str++ = NULLC;
  200.     return start;
  201.       }
  202.   
  203.   /* reached the end of the string */
  204.   return start;
  205. }
  206. #endif /* HAVE_STRTOK == 0 */
  207.